const app =getApp();
Page({
  data: {
    id:0,
    user:'',
    datailData:[],
    boxsList:[],
  },
  //监听页面加载
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.data.id = options.id
    this.getseriesData(this.data.id )
  },
  // 监听页面显示
  onShow: function () {},
  //选择收货地址
  lookAddress(){
    // 正确流程：
    // 1.获取 权限状态
      wx.getSetting({
        success: (result) => {
          // 2. 获取权限状态 
          // 主要发现一些 属性名很怪异的时候 都要使用 [] 形式来获取属性
            const scopeAddres=result.authSetting["scope.address"];
          // 判断用户的权限状态
            if(scopeAddres===true||scopeAddres===undefined){
              wx.chooseAddress({
                success: (result1) => {
                  //获取数据
                  this.setData({
                    user:{
                      name:result1.userName,
                      address:result1.cityName + result1.countyName +result1.detailInfo,
                      tel:result1.telNumber
                    }
                  })
                }
              });
            }
            else{
              // 3. 用户 以前拒绝过授予权限  先引导用户打开权限页面
              wx.openSetting({
                success: (result2) => {
                  // 4.可以调用  获取收获地址代码
                  wx.chooseAddress({
                    success: (result3) => {
                      // 获取数据
                      this.setData({
                        user:{
                          name:result3.userName,
                          cityName:result3.cityName
                        }
                      })
                    }
                  });
                }
              });
            }
          }
      });
  },
  //获取盲盒信息
  getseriesData(id){
    var that = this;
    app.util.request({
      url: app.do('index/getSeries'),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: id,
      },
      success: function (res) {
        if (res.data.code == "0000") {
          that.setData({
            datailData: res.data.data.seriesDetail,
            boxsList:res.data.data.newData,
          })
          console.log('datailData',that.data.datailData)
        } 
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  
  //确认支付
  paymentBtn(){
    let token = wx.getStorageSync('token')
    var that = this;
    if(!that.data.user.name){
      wx.showToast({
        title: '请选择收货地址',
        icon:'none'
      });
      return;
    }
    wx.showModal({
      title: '',
      content: '确定要支付吗？',
      success (res) {
        if (res.confirm) {
          app.util.request({
            url: app.do('index/getPayment'),
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              token: token,
              typeId:that.data.id,
              address:that.data.user.address,
              mobile:that.data.user.tel,
              realname:that.data.user.name,
              totalPrice:that.data.datailData.endPrice,
              totalSum:9
            },
            success: function(res) {
              if(res.data.code == "0000"){
                  let jsdk = res.data.data.jsdk;
                  //调用微信支付
                  that.pay(jsdk);
              }
              else if(res.data.code == "2001"){
                wx.navigateTo({
                  url: '../auth/auth',
                })
              }
              else{
                var text = res.data.message;
                wx.showToast({
                  title: text,
                  icon: 'none',
                  duration: 2000,
                })
              }
            },
            fail: function(res) {
              var text = res.data.message;
              wx.showToast({
                title: text,
                icon: 'none',
                duration: 2000,
              })
            }
          })
        }
        else if (res.cancel) { }
      }
    })

  },
  pay(jsdk){
    console.log('fff')
    wx.requestPayment({
      nonceStr: jsdk.nonceStr,
      package: jsdk.package,
      paySign: jsdk.paySign,
      signType: 'MD5',
      timeStamp: jsdk.timeStamp,
      success (res) { 
        wx.switchTab({
          url: '../center/center',
        })
      },
      fail (res) { }
    })
  },
})